var netWork = require('../../utils/netWork.js');
const app = getApp();
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imagesUrl: getApp().globalData.imagesUrl,
    topHeight: 0, //顶部高度
    top: 0,
    isShowMore: false, //显示引导点击更多
    isShowPub: false, //显示引导点击关注
    isFollow: -1, //是否关注
    isQuestMask:false,//显示问卷调查
    prizeInfo: "",
    questPage:1,//问卷第1页
    questionAnswer:{"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""},
    prizeOrderId:"",
    isFinsh:false,
    star1:0,
    star2:0,
    star3:0,
    star4:0,
    goToPage:2,//1-中奖记录，2-关注公众号

  },
  onShow:function(){
    //其他页面返回时，需要重新请求一下。
    if(this.data.isFollow!=-1){
      //不是首次，进行查询一下
      const that = this;
      netWork.POST('searchUserInfo', {
        data: {
          userId: wx.getStorageSync('userId')
        },
        success: function (res) {
          api.cancelLoading();
          if (res.data.retCode == undefined) {
            wx.redirectTo({
              url: '../../prompt/prompt?retCode=' + apiCode.FAIL.code + '&retMsg=' + apiCode.FAIL.msg,
            })
          }
          if (res.data.retCode == "0000") {
            if (res.data.data.isFollow == 1) {
              that.setData({
                goToPage: 1
              })
            } else {
              that.setData({
                goToPage: 2,
              })
            }
           
          }
        },
        fail: function () {
          // wx.redirectTo({
          //   url: '../../prompt/prompt?retCode=' + apiCode.DRAWFAIL.code + '&retMsg=' + apiCode.DRAWFAIL.msg,
          // })
        }
      });
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("onload======")
    api.showLoading('正在加载中...')
    api.judgeNetworkStatus(function (res) {
      if ("none" == res) return;
    })
    let topHeight = wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight');
    this.setData({
      topHeight: topHeight
    });
    if (options.prizeOrderId && options.prizeOrderId != "") {
      this.setData({
        prizeOrderId: options.prizeOrderId
      })
    }

    if (options.next && options.next != "") {
      let prizeInfo = JSON.parse(options.next);
      if (prizeInfo != "" && prizeInfo != null && prizeInfo != undefined) {
        //判断C奖二选一且只有一个的时候，是否存在未中奖
        if (prizeInfo.schemeList != null && prizeInfo.schemeList != "" && prizeInfo.schemeList.length == 1) {
          if (prizeInfo.schemeList[0].prizeType != 8) {
            this.setData({
              prizeInfo: JSON.parse(options.next)
            })
          }
        } else {
          if (prizeInfo.prizeType != 8) {
            this.setData({
              prizeInfo: JSON.parse(options.next)
            })
          }
        }
      }else{
        console.info("不是继续领取")
        //不是继续领取
        const that = this;
        //查询是否为金蝶会员
        setTimeout(function () {
          that.queryKingVIPMember();
        }, 500);
        
      }
    }else{
      console.info("不是继续领取")
      //不是继续领取
      const that = this;
      //查询是否为金蝶会员
      setTimeout(function () {
        that.queryKingVIPMember();
      }, 500);
      
    }


    
    
  },
  /**
   *查询是否为金蝶会员
   */
  queryKingVIPMember: function () {
    api.showLoading('正在加载中...')
    const that = this;
      netWork.POST('xhtx/little/queryKingVIPMember', {
        data: {
          userId: wx.getStorageSync('userId'),
          prizeOrderId: that.data.prizeOrderId
        },
        success: function (res) {
          if (res.data.retCode == "0000") {
            if (res.data.data.cardNo!="") {
              that.setData({
                isQuestMask: true,
                cardNo:res.data.data.cardNo
              });
              return;
            } else{
              that.queryIsFollow();
            }
          }else{
            that.queryIsFollow();
          }
        },
        fail: function () {
          that.queryIsFollow();
        }
      });
    
   
    
  },

  /**
   *增加积分
   */
  saveAnswer: function () {
    const that = this;
    netWork.POST('xhtx/little/saveAnswer', {
      data: {
        userId: wx.getStorageSync('userId'),
        cardNo:that.data.cardNo,//卡号
        questionAnswer:JSON.stringify(that.data.questionAnswer),//答案
        prizeOrderId: that.data.prizeOrderId
      },
      success: function (res) {
         
      },
      fail: function () {
        
      }
    });
    
  },

    /**
   *查询是否关注公众号
   */
  queryIsFollow: function () {
    const that = this;
    netWork.POST('searchUserInfo', {
      data: {
        userId: wx.getStorageSync('userId')
      },
      success: function (res) {
        api.cancelLoading();
        if (res.data.retCode == undefined) {
          wx.redirectTo({
            url: '../../prompt/prompt?retCode=' + apiCode.FAIL.code + '&retMsg=' + apiCode.FAIL.msg,
          })
        }
        if (res.data.retCode == "0000") {
          if (res.data.data.isFollow == 1) {
            that.setData({
              isFollow: 1,
              isShowMore:true,
              goToPage:1
            })
          } else {
            that.setData({
              isFollow: 0,
              goToPage:2
            })
          }
          // if (res.data.data.activityId != null && res.data.data.activityId != "") {
          //   wx.setStorageSync('userType', "2");
          // }
        }
      },
      fail: function () {
 
      }
    });
  },
  onReady: function () {
    
  },
 
  /**
   * 跳转中奖记录页面
   */
  showPrizeRecord: function () {

    if(this.data.goToPage==2){
      wx.navigateTo({//未关注
        url: '../officialAccount/officialAccount',
      })
    }else{//关注
      wx.redirectTo({
        url: '../prizeRecord/prizeRecord',
      })
    }
 
  },
  /**
   * 更多活动， 跳转第三方小程序
   */
  showMore: function () {
    wx.navigateToMiniProgram({
      appId: api.thirdAppId,
    })
  },
  /**
   * 继续领取
   */
  conRevicePrize: function () {
    api.showLoading('正在加载中...')
    // 判断类型
    let prizeInfo = this.data.prizeInfo;
    wx.redirectTo({
      url: '../prize/prizeSecond/prizeSecond?source=1&prizeInfo=' + JSON.stringify(prizeInfo),
    })
  },
  onReady: function () {
    api.cancelLoading();
    const that = this;
    if (that.data.prizeInfo != '') {
      return;
    }

    let query = wx.createSelectorQuery();
    query.select('.bnt-rm').boundingClientRect(rect => {
      let clientWidth = rect.width;
      let ratio = 750 / clientWidth;
      let clientTop = rect.top;
      let top = clientTop * ratio - 350;
      setTimeout(function () {
        that.setData({
          isShowPub: true
        })
      }, 500);
      that.setData({
        top: top,
      })
    }).exec();
  },
  closeMask() {
    this.setData({
      isShowMore: false
    })


  },
  closeMaskPub() {
    this.setData({
      isShowPub: false
    })
    const that = this;
    let query = wx.createSelectorQuery();
    query.select('.bnt-rm').boundingClientRect(rect => {
      let clientWidth = rect.width;
      let ratio = 750 / clientWidth;
      let clientTop = rect.top;
      let top = clientTop * ratio - 350;
      setTimeout(function () {
        that.setData({
          isShowMore: true
        })
      }, 500);
      that.setData({
        top: top,
      })
    }).exec();

  },
  // 关注公众号
  gzPubnum() {
    api.showLoading('正在加载中...')
    setTimeout(function () {
      wx.navigateTo({
        url: '../officialAccount/officialAccount',
      })
    }, 500);
  },
  //问卷第1页关闭
  close1Page(){
    this.setData({
      isQuestMask: false,
      questPage:1
    })
    const that = this;
    setTimeout(function () {
      //问卷完成，引导关注
      that.queryIsFollow();
    }, 1000)
  },
  //goto问卷第2页
  goto2Page(){
   
    //去下一页
    this.setData({
      questPage:2
    })
    
    
  },
  //goto问卷第3页
  goto3Page(){
     //非空校验
     if(this.data.index==null||this.data.index==undefined
      ||this.data.index2==null||this.data.index2==undefined||
      this.data.index3==null||this.data.index3==undefined){
      setTimeout(function () {
        wx.showToast({
          title: '您还有题目未答完哦~',
          icon: 'none'
        })
      }, 100)
      return;
    }
    this.setData({
      questPage:3
    })
    var temp = this.data.questionAnswer;
    temp[1]=this.data.index;
    temp[2]=this.data.index2;
    temp[3]=this.data.index3;
    //拼接答案
    this.setData({
      questionAnswe:temp
    })
  },
  //goto问卷第4页
  goto4Page(){
    //非空校验
    if(this.data.index4==null||this.data.index4==undefined
     ||this.data.index5==null||this.data.index5==undefined||
     this.data.index6==null||this.data.index6==undefined){
     setTimeout(function () {
       wx.showToast({
         title: '您还有题目未答完哦~',
         icon: 'none'
       })
     }, 100)
     return;
   }
   this.setData({
     questPage:4
   })
   var temp = this.data.questionAnswer;
   temp[4]=this.data.index4;
   temp[5]=this.data.index5;
   temp[6]=this.data.index6;
   //拼接答案
   this.setData({
     questionAnswe:temp
   })
 },
  
  //goto问卷第5页
  goto5Page(){
    //非空校验
    if(this.data.star1==0||this.data.star2==0||this.data.star3==0||this.data.star4==0){
      setTimeout(function () {
        wx.showToast({
          title: '您还有题目未答完哦~',
          icon: 'none'
        })
      }, 100)
      return;
    }
    this.setData({
      questPage:5
    })
    //提交的答案
    var temp = this.data.questionAnswer;
    temp[7]=this.data.star1+","+this.data.star2+","+this.data.star3+","+this.data.star4;
    
    //拼接答案
    this.setData({
      questionAnswe:temp
    })
    //请求金蝶增加积分
    this.saveAnswer();
    

  },
  //第1题的点击事件
  goIndex1(event){
    let index= event.currentTarget.dataset.index;
    this.setData({
      index:index
    })
  },
  //第2题的点击事件
  goIndex2(event){
    let index= event.currentTarget.dataset.index;
    this.setData({
      index2:index
    })
  },
  //第3题的点击事件
  goIndex3(event){
      let index= event.currentTarget.dataset.index;
      this.setData({
        index3:index
      })
    },
 //第4题的点击事件
 goIndex4(event){
  let index= event.currentTarget.dataset.index;
  this.setData({
    index4:index
  })
  },
  //第5题的点击事件
 goIndex5(event){
  let index= event.currentTarget.dataset.index;
  this.setData({
    index5:index
  })
  },
  //第6题的点击事件
 goIndex6(event){
  let index= event.currentTarget.dataset.index;
  this.setData({
    index6:index
  })
  },
    //第7题的点击事件
 goIndex7(event){
  let index= event.currentTarget.dataset.index;
  this.setData({
    index7:index
  })
  },
  //问卷完成
  finishQuest(){
    this.setData({     
      isFinsh:true
    });
    const that = this;
    setTimeout(function () {
      that.setData({
        isQuestMask:false
      });
    }, 1000)
    setTimeout(function () {
      //问卷完成，引导关注
      that.queryIsFollow();
    }, 2000)
    
    
  },
  adviceMsgInput(e){
    var temp = this.data.questionAnswer;
    var value = e.detail.value.length;//输入内容的长度
    var lastArea = 100 - value;//剩余字数
    temp[8]=e.detail.value;
    this.setData({
      questionAnswer:temp,
      lastArea: lastArea
    })
  },
  changeStar( event){
    let star1= event.currentTarget.dataset.star1;//第一组星星
    let star2= event.currentTarget.dataset.star2;//第二组星星
    let star3= event.currentTarget.dataset.star3;//第三组星星
    let star4= event.currentTarget.dataset.star4;//第四组星星

    if(star1!=undefined){
      this.setData({
        star1:star1,
        move1:this.data.move1?false:true
      })
    }
    if(star2!=undefined){
      this.setData({
        star2:star2,
        move2:this.data.move2?false:true
      })
    }
    if(star3!=undefined){
      this.setData({
        star3:star3,
        move3:this.data.move3?false:true
      })
    }
    if(star4!=undefined){
      this.setData({
        star4:star4,
        move4:this.data.move4?false:true
      })
    }
   
  }
  
})